package com.coco.bargain.mq;

import com.coco.bargain.mapper.BargainGoodsMapper;
import com.coco.bargain.mapper.BargainItemMapper;
import com.coco.bargain.mapper.BargainUserHelpMapper;
import com.coco.bargain.mapper.BargainUserMapper;
import com.coco.bargain.pojo.TbBargainGoods;
import com.coco.bargain.pojo.TbBargainItem;
import com.coco.bargain.pojo.TbBargainUser;
import com.coco.bargain.pojo.TbBargainUserHelp;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: guofeng
 * @Date: 2021/07/21/14:22
 * @Description:
 */
@Component
public class BargainQueue {

    @Autowired
    private BargainUserMapper bargainUserMapper;

    @Autowired
    private BargainGoodsMapper bargainGoodsMapper;

    @Autowired
    private BargainItemMapper bargainItemMapper;

    @Autowired
    private BargainUserHelpMapper bargainUserHelpMapper;

    /**
     * 商品记录添加
     * @param bargainUserRecord
     */
    @RabbitListener(queues = "bargainRecordAdd")
    @RabbitHandler
    public void bargain(TbBargainUser bargainUserRecord) {
        bargainUserMapper.add(bargainUserRecord);
    }

    /**
     * 商品砍价失败
     * 商品回调加1
     * @param skuId
     */
    @RabbitListener(queues = "bargainShopRollback")
    @RabbitHandler
    public void bargainShopRollback(Long skuId) {
        //sku商品信息回调
        TbBargainItem user = bargainItemMapper.findById(skuId);
        user.setResidueNum(user.getResidueNum()+1);
        bargainItemMapper.update(user);

        //砍价商品回调(spu)
        TbBargainGoods good = bargainGoodsMapper.findOne(user.getBargainGoodsId());
        good.setResidueNum(good.getResidueNum()+1);
        bargainGoodsMapper.updateById(good);
    }


    @RabbitListener(queues = "helpMeBargainUserAdd")
    @RabbitHandler
    public void helpMeBargainUserAdd(TbBargainUserHelp tbBargainUserHelp) {
        //添加帮砍记录
        bargainUserHelpMapper.add(tbBargainUserHelp);
        //修改帮砍次数
        TbBargainGoods bargainGoods = bargainGoodsMapper.findOne(tbBargainUserHelp.getBargainItemId());
        bargainGoods.setHelpPersonNum(bargainGoods.getHelpPersonNum()+1);
        bargainGoodsMapper.updateById(bargainGoods);
    }


    @RabbitListener(queues = "bargainOk")
    @RabbitHandler
    public void bargainOk(Long gid) {
        //砍价成功,次数加1

        System.out.println(gid);
        TbBargainGoods bargainGoods = bargainGoodsMapper.findOne(gid);
        System.out.println(bargainGoods);
        bargainGoods.setSuccessNum(bargainGoods.getSuccessNum()+1);
        System.out.println(bargainGoods);
        bargainGoodsMapper.updateById(bargainGoods);
    }


    @RabbitListener(queues = "bargainUserOk")
    @RabbitHandler
    public void bargainUserOk(String card) {
        //用户砍价成功
        TbBargainUser byCard = bargainUserMapper.findByCard(card);
        byCard.setStatus(3);
        bargainUserMapper.update(byCard);
    }

    @RabbitListener(queues = "userFirstBargain")
    @RabbitHandler
    public void userFirstBargain(TbBargainUser user) {
        //用户第一次修改
        bargainUserMapper.update(user);
    }
}
